package com.qlk.odcinterface.rest;

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import com.qlk.common.utils.wrap.WrapMapper;
import com.qlk.common.utils.wrap.Wrapper;
import com.qlk.odcinterface.domain.PmsFullCutItem;
import com.qlk.odcinterface.sdk.api.request.PmsFullCutItemRequest;
import com.qlk.odcinterface.sdk.api.response.PmsFullCutItemResponse;
import com.qlk.odcinterface.service.PmsFullCutItemService;

/**
 * 单品促销明细表REST服务：提供有关单品促销明细表的接口
 * 
 * @author zhaoyanan
 * @version 2016-4-14 13:22:59
 * 
 */
@Component
@Path("services")
@Produces({ MediaType.APPLICATION_JSON })
@Consumes({ MediaType.APPLICATION_JSON })
public class PmsFullCutItemResource {

    private final Log logger = LogFactory.getLog(this.getClass());

    @Autowired
    private PmsFullCutItemService pmsFullCutItemService; 

    /**
     * 查询单品促销明细表信息
     * 
     * @param request
     *            单品促销明细表请求参数
     * @return 单品促销明细表返回对象
     * 
     */
    @POST
    @Path("/pmsFullCutItem/getPmsFullCutItem")
    public Wrapper<?> getPmsFullCutItem(PmsFullCutItemRequest request) {
        if (null == request || null == request.getId()) {
            this.logger.error("getPmsFullCutItem 传入参数有误");
            return WrapMapper.illegalArgument();
        }

        try {
            PmsFullCutItem pmsFullCutItem = pmsFullCutItemService.getPmsFullCutItemById(request.getId());
            PmsFullCutItemResponse response = convert(pmsFullCutItem);
            return WrapMapper.ok().result(response);
        } catch (Exception e) {
            this.logger.error("查询单品促销明细表数据异常", e);
            return WrapMapper.error();
        }
    } 

    // 数据转换
    private PmsFullCutItemResponse convert(PmsFullCutItem pmsFullCutItem) {
        if (null == pmsFullCutItem) {
            return null;
        }

        PmsFullCutItemResponse pmsFullCutItemResponse = new PmsFullCutItemResponse();
        BeanUtils.copyProperties(pmsFullCutItem, pmsFullCutItemResponse);
        return pmsFullCutItemResponse;
    }

    // 数据转换
    private List<PmsFullCutItemResponse> convertList(List<PmsFullCutItem> pmsFullCutItems) {
        if (CollectionUtils.isEmpty(pmsFullCutItems)) {
            return null;
        }

        List<PmsFullCutItemResponse> list = new ArrayList<PmsFullCutItemResponse>(pmsFullCutItems.size());
        for (PmsFullCutItem pmsFullCutItem : pmsFullCutItems) {
            list.add(convert(pmsFullCutItem));
        }
        return list;
    } 

}
